package com.sinosoft.hydra.um.domain.permission;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.*;
import cn.com.sinux.spring.utils.ValidatorUtils;
import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_E_BOM")
public class TEBom implements Serializable {
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.id
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @Size(max = 50, message = "tebom.id.max.overflow")
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "ID")
    private String id;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.code
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @NotNull(message = "tebom.code.not.null")
    @Size(max = 50, message = "tebom.code.max.overflow")
    @Column(name = "CODE")
    private String code;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.e_name
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @NotNull(message = "tebom.ename.not.null")
    @Size(max = 200, message = "tebom.ename.max.overflow")
    @Column(name = "E_NAME")
    private String eName;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.node_type
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @NotNull(message = "tebom.bomtype.not.null")
    @Size(max = 50, message = "tebom.bomtype.max.overflow")
    @Column(name = "NODE_TYPE")
    private String bomType;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.description
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @Size(max = 400, message = "tebom.description.max.overflow")
    @Column(name = "DESCRIPTION")
    private String description;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.cover
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @Size(max = 200, message = "tebom.cover.max.overflow")
    @Column(name = "COVER")
    private String cover;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.remark
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @Size(max = 400, message = "tebom.remark.max.overflow")
    @Column(name = "REMARK")
    private String remark;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.create_time
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @NotNull(message = "tebom.createtime.not.null")
    @Column(name = "CREATE_TIME")
    @JSONField(format = "yyyy-MM-dd")
    private Date createTime;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.create_user
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CREATE_USER")
    private SysUserMember createUser;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.status
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @NotNull(message = "tebom.status.not.null")
    @Size(max = 50, message = "tebom.status.max.overflow")
    @Column(name = "STATUS")
    private String status;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom.p_bom
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "P_BOM")
    private TPBom tPBom;

    @NotNull(message = "tebom.used.not.null")
    @Column(name = "USED")
    private Integer used;

    public Integer getUsed() {
        return used;
    }

    public void setUsed(Integer used) {
        this.used = used;
    }

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_e_bom
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    private static final long serialVersionUID = 1L;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_e_bom_relation.ref_id
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    @OneToMany(mappedBy = "ref", fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    private List<TEBomRelation> tEBomRelations;


    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY, mappedBy = "teBoms")
    private List<TProcessRecord> tProcessRecords;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.id
     *
     * @return the value of t_e_bom.id
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.id
     *
     * @param id the value for t_e_bom.id
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.code
     *
     * @return the value of t_e_bom.code
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getCode() {
        return code;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.code
     *
     * @param code the value for t_e_bom.code
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.e_name
     *
     * @return the value of t_e_bom.e_name
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String geteName() {
        return eName;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.e_name
     *
     * @param eName the value for t_e_bom.e_name
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void seteName(String eName) {
        this.eName = eName;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.node_type
     *
     * @return the value of t_e_bom.node_type
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getBomType() {
        return bomType;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.node_type
     *
     * @param bomType the value for t_e_bom.node_type
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setBomType(String bomType) {
        this.bomType = bomType;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.description
     *
     * @return the value of t_e_bom.description
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getDescription() {
        return description;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.description
     *
     * @param description the value for t_e_bom.description
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.cover
     *
     * @return the value of t_e_bom.cover
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getCover() {
        return cover;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.cover
     *
     * @param cover the value for t_e_bom.cover
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setCover(String cover) {
        this.cover = cover;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.remark
     *
     * @return the value of t_e_bom.remark
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getRemark() {
        return remark;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.remark
     *
     * @param remark the value for t_e_bom.remark
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.create_time
     *
     * @return the value of t_e_bom.create_time
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.create_time
     *
     * @param createTime the value for t_e_bom.create_time
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.create_user
     *
     * @return the value of t_e_bom.create_user
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public SysUserMember getCreateUser() {
        return createUser;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.create_user
     *
     * @param createUser the value for t_e_bom.create_user
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setCreateUser(SysUserMember createUser) {
        this.createUser = createUser;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.status
     *
     * @return the value of t_e_bom.status
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public String getStatus() {
        return status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.status
     *
     * @param status the value for t_e_bom.status
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom.p_bom
     *
     * @return the value of t_e_bom.p_bom
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public TPBom gettPBom() {
        return tPBom;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom.p_bom
     *
     * @param tPBom the value for t_e_bom.p_bom
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void settPBom(TPBom tPBom) {
        this.tPBom = tPBom;
    }

    @Transient
    private boolean bomGroup;

    public boolean isBomGroup() {
        return bomGroup;
    }

    public void setBomGroup(boolean bomGroup) {
        this.bomGroup = bomGroup;
    }

    public List<TProcessRecord> gettProcessRecords() {
        return tProcessRecords;
    }

    public void settProcessRecords(List<TProcessRecord> tProcessRecords) {
        this.tProcessRecords = tProcessRecords;
    }

    @Transient
    private Integer nodeLevel;

    public Integer getNodeLevel() {
        return nodeLevel;
    }

    public void setNodeLevel(Integer nodeLevel) {
        this.nodeLevel = nodeLevel;
    }

    @Transient
    public String getPBomDes(){
        if(gettPBom() == null){
            return "";
        }
        return gettPBom().getpName() + "(" + gettPBom().getCode() + ")";
    }

    @Transient
    public boolean isDoProcess(){
        return ValidatorUtils.isArrayNotEmpty(gettProcessRecords());
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_e_bom
     *
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", code=").append(code);
        sb.append(", eName=").append(eName);
        sb.append(", bomType=").append(bomType);
        sb.append(", description=").append(description);
        sb.append(", cover=").append(cover);
        sb.append(", remark=").append(remark);
        sb.append(", createTime=").append(createTime);
        sb.append(", createUser=").append(createUser);
        sb.append(", status=").append(status);
        sb.append(", tPBom=").append(tPBom);
        sb.append("]");
        return sb.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_e_bom_relation.ref_id
     *
     * @return the value of t_e_bom_relation.ref_id
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public List<TEBomRelation> gettEBomRelations() {
        return tEBomRelations;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_e_bom_relation.ref_id
     *
     * @param tEBomRelations the value for t_e_bom_relation.ref_id
     * @mbggenerated Sat Jul 09 15:41:30 CST 2016
     */
    public void settEBomRelations(List<TEBomRelation> tEBomRelations) {
        this.tEBomRelations = tEBomRelations;
    }

    @Transient
    private String refId;

    public String getRefId() {
        return refId;
    }

    public void setRefId(String refId) {
        this.refId = refId;
    }

    public String getState() {
        return ValidatorUtils.isArrayNotEmpty(gettEBomRelations()) ? "closed" : "";
    }

    public void setChildren(List<TEBom> teBoms) {
        this.tEBomRelations = new ArrayList<TEBomRelation>(teBoms.size());
        if (ValidatorUtils.isArrayEmpty(teBoms)) {
            return;
        }
        for (TEBom teBom : teBoms) {
            TEBomRelation teBomRelation = new TEBomRelation();
            teBomRelation.setChild(teBom);
            teBomRelation.setStatus(teBom.getStatus());
            teBomRelation.setRef(this);
            tEBomRelations.add(teBomRelation);
        }
    }

    public TEBom addChild(TEBom teBom) {
        if (this.tEBomRelations == null) {
            this.tEBomRelations = new ArrayList<TEBomRelation>();
        }
        boolean find = false;
        if (StringUtils.isNotBlank(teBom.getCode())) {
            for (TEBomRelation teBomRelation : this.tEBomRelations) {
                if (teBom.getCode().equals(teBomRelation.getChild().getCode())) {
                    find = true;
                    break;
                }
            }
            if (!find) {
                TEBomRelation teBomRelation = new TEBomRelation();
                teBomRelation.setStatus(teBom.getStatus());
                teBomRelation.setChild(teBom);
                teBomRelation.setRef(this);
                this.tEBomRelations.add(teBomRelation);
            }
        }
        return this;
    }

    public List<TEBom> getChildren() {
        List<TEBom> teBoms = new ArrayList<TEBom>();
        if (ValidatorUtils.isArrayNotEmpty(tEBomRelations)) {
            for (TEBomRelation teBomRelation : tEBomRelations) {
                TEBom teBom = teBomRelation.getChild();
                teBom.setStatus(teBom.getStatus());
                teBomRelation.setRef(this);
                teBoms.add(teBom);
            }
        }
        return teBoms;
    }
}